#!/usr/bin/env python
# -*- conding:utf-8 -*-
# __Author__:'liudong'
import os
import pickle
import sys
sys.path.append('..\\core\\')
import show_log

def save_money(user, amount):
    """
    存款功能
    :param user:用户名
    :param amount: 存款额
    :return:
    """
    save_file = open('..\\db\\user.db', 'rb')  # 打开文件读取用户信息
    save_user_userinfo = pickle.load(save_file)
    # print(save_user_userinfo,type(save_user_userinfo))
    save_file.close()
    save_user_userinfo[user][4] += str(int(save_user_userinfo[user][4])+int(amount))
    new_save_file = open('..\\db\\newuser.db', 'wb')
    pickle.dump(save_user_userinfo, new_save_file)
    new_save_file.close()
    os.remove('..\\db\\user.db')
    os.renames('..\\db\\newuser.db', '..\\db\\user.db')  # 还要增加日志
    #log_write = log_func.save_user_log(user)
    return True

def withdraw_money(user, amount):
    """
    用户取款功能
    :param user:
    :param amount:
    :return:
    """
    withdraw_file = open('..\\db\\user.db', 'rb')  # 打开文件读取用户信息
    withdraw_userinfo = pickle.load(withdraw_file)
    # print(save_user_userinfo,type(save_user_userinfo))
    withdraw_file.close()
    withdraw_userinfo[user][3] = str(int(withdraw_userinfo[user][3]) - int(amount))
    new_save_file = open('..\\db\\newuser.db', 'wb')
    pickle.dump(withdraw_userinfo, new_save_file)
    new_save_file.close()
    os.remove('..\\db\\user.db')
    os.renames('..\\db\\newuser.db', '..\\db\\user.db')  # 还要增加日志
    # log_write = log_func.save_user_log(user)
    return True

def payback_money(user, amount):
    """
    用户还款功能
    :param user:
    :param amount:
    :return:
    """
    payback_file = open('..\\db\\user.db', 'rb')  # 打开文件读取用户信息
    payback_userinfo = pickle.load(payback_file)
    # print(save_user_userinfo,type(save_user_userinfo))
    payback_file.close()
    payback_userinfo[user][3] = str(int(payback_userinfo[user][3])+int(amount))
    new_payback_file = open('..\\db\\newuser.db', 'wb')
    pickle.dump(payback_userinfo, new_payback_file)
    new_payback_file.close()
    os.remove('..\\db\\user.db')
    os.renames('..\\db\\newuser.db', '..\\db\\user.db')  # 还要增加日志
    # log_write = log_func.save_user_log(user)
    return True

def transfor_money(user_out, user_in, amount):
    """
    转账功能
    :param user_out:
    :param user_in:
    :param amount:
    :return:
    """
    tran_file = open('..\\db\\user.db', 'rb')  # 打开文件读取用户信息
    tran_userinfo = pickle.load(tran_file)
    tran_file.close()
    tran_userinfo[user_out][2] = str(int(tran_userinfo[user_out][2])-int(amount))
    tran_userinfo[user_in][2] = str(int(tran_userinfo[user_in][2])+int(amount))
    new_tran_file = open('..\\db\\newuser.db', 'wb')
    pickle.dump(tran_userinfo, new_tran_file)
    new_tran_file.close()
    os.remove('..\\db\\user.db')
    os.renames('..\\db\\newuser.db', '..\\db\\user.db')  # 还要增加日志
    # log_write = log_func.save_user_log(user)
    return True